﻿using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore;
using System.Text;
using System.Threading.Tasks;

namespace SingleBlog.Framework.Middlewares
{
    /// <summary>
    /// 检测安装的中间件。
    /// </summary>
    public class InstallationMiddleware
    {
        private readonly RequestDelegate _next;
        public InstallationMiddleware(RequestDelegate next
            )
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context, DbContext dbContext)
        {
            if (!dbContext.DatabaseExist())
            {
                if (!context.Request.Path.Value.ToLower().Contains("install"))
                {
                    await context.Response.WriteAsync(
                         $@"
                            <html>
                            Please click <a href='/install'>installation</a> to install.
                            </html>
                            "
      , Encoding.UTF8);
                }
            }
            await _next(context);
        }
    }
}
